<?php
/*
	[UCenter Home] (C) 2007-2008 Comsenz Inc.
	$Id: cp_blog.php 13026 2009-08-06 02:17:33Z liguode $
*/

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}

if ($op=='localimage') {
	if ($_POST['images']) {
		$fliename = time().mt_rand(1,9999);
		$ext = trim(substr(strrchr($_POST['images'], '.'), 1));
		$allowExt = array('gif', 'png', 'jpg', 'jpeg', 'bmp');
		if (!in_array($ext,$allowExt)) {
			echo '对不起，你上传的不是图片！';exit();
		}
		
		$filepath = getfilepath($ext, true);
		$moviemgname = $_SC['attachdir'].'./'.$filepath;
		//$dir = $_SC['attachdir']."./movie";
		//$moviemgname = $dir.'/'.$fliename.'.'.$ext;
		//if (!is_dir($dir)) {
		//	mkdir($dir,0777);
		//}
		if (file_put_contents($moviemgname,file_get_contents($_POST['images']))) {
			echo $moviemgname;
		}else {
			echo '本地化失败';
		}
		
	}
	exit();
}

$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_moviecredit')." WHERE type='movie'");
$value = $_SGLOBAL['db']->fetch_array($query);
$_SGLOBAL[moviecredit]=$value['sendmovie'];
//var_dump($_SGLOBAL[moviecredit]);

//检查信息
$movieid = empty($_GET['movieid'])?0:intval($_GET['movieid']);
$op = empty($_GET['op'])?'':$_GET['op'];

$movie = array();
$typelist = $regionlist = $playlist = array();

if($movieid) {
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie')." AS movie 
		LEFT JOIN ".tname('app_movieurl')." AS url ON movie.movieid=url.movieid 
		WHERE movie.movieid='$movieid'");
	$movie = $_SGLOBAL['db']->fetch_array($query);
}

//分类列表
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_type')." ORDER BY sort ASC");
while ($row= $_SGLOBAL['db']->fetch_array($query)) {
	$typelist[] = $row;
}
//var_dump($typelist);
//地区列表
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_region')." ORDER BY sort ASC");
while ($row= $_SGLOBAL['db']->fetch_array($query)) {
	$regionlist[] = $row;
}
//var_dump($regionlist);
//播放器列表
$query = $_SGLOBAL['db']->query("SELECT playid,playername FROM ".tname('app_movie_plays')." ORDER BY sort ASC");
while ($row= $_SGLOBAL['db']->fetch_array($query)) {
	$playlist[] = $row;
}
//var_dump($playlist);
//权限检查
if(empty($movie)) {
	if(!checkperm('allowmovie')) {
		ckspacelog();
		showmessage('对不起，你没有权限');
	}
	//实名认证
	ckrealname('movie');
	
	//视频认证
	ckvideophoto('movie');
	
	//新用户见习
	cknewuser();
	
	//判断是否发布太快
	$waittime = interval_check('post');
	if($waittime > 0) {
		showmessage('operating_too_fast','',1,array($waittime));
	}
	
} else {
	
	if($_SGLOBAL['supe_uid'] != $movie['uid'] && !checkperm('managemovie')) {
		showmessage('对不起，你没有权限操作');
	}
}
$albums = getalbums($_SGLOBAL['supe_uid']);
//添加编辑操作
if(submitcheck('moviesubmit')) {
	//var_dump($_POST);exit();
	if(!checkperm('allowmovie')) {
		ckspacelog();
		showmessage('对不起，你没有权限操作');
	}
	
	if ($movie[checked]==1) {
		$_SGLOBAL['db']->query("UPDATE ".tname('space')." SET credit=credit-$_SGLOBAL[moviecredit] WHERE uid='$movie[uid]'");
		$_SGLOBAL['db']->query("UPDATE ".tname('app_movie_sendusers')." SET sendnum=sendnum-1 WHERE uid='$movie[uid]' AND sendnum > 0");
	}
	$data[mname] = $_POST[mname];
	$data[typeid] = $_POST[typeid];
	$data[regionid] = $_POST[regionid];
	$data[attribute] =  $_POST[attribute];
	$data[dateline] = time(); 
	if($_POST[datetime]){
		$year=((int)substr($_POST[datetime],0,4));//取得年份
		$data[dateline] = mktime(0,0,0,1,rand(0,12),$year); 
	} 
	//保存远程图片
	if ($_POST['localimage']) {
		if ($_POST['images']) {
			$fliename = time().mt_rand(1,9999);
			$ext = trim(substr(strrchr($_POST['images'], '.'), 1));
			$allowExt = array('gif', 'png', 'jpg', 'jpeg', 'bmp');
			if (!in_array($ext,$allowExt)) {
				$data[images]="";
			}
			$filepath = getfilepath($ext, true);
			$moviemgname = $_SC['attachdir'].'./'.$filepath;
			if (file_put_contents($moviemgname,file_get_contents($_POST['images']))) {
				$data[images] = $moviemgname;
				//缩略图
				include_once(S_ROOT.'./source/function_image.php');
				$thumbpath = makethumb($moviemgname);
				$thumb = empty($thumbpath)?0:1;			
				$setarr = array(
					'albumid' => 0,
					'uid' => $_SGLOBAL['supe_uid'],
					'username' => $_SGLOBAL['supe_username'],
					'dateline' => $_SGLOBAL['timestamp'],
					'filename' => addslashes($fliename),
					'postip' => getonlineip(),
					'title' => $data[mname],
					'type' => addslashes($ext),
					'size' => filesize($moviemgname),
					'filepath' => $filepath ,
					'thumb' => $thumb,
					'remote' => 0,
					'topicid' => 0
				);
				$setarr['picid'] = inserttable('pic', $setarr, 1);
			}else {
				$data[images]="";
			}
		} 
	}else{
		$data[images] = $_POST[images];	
	}
	$data[actor] = $_POST[actor];
	$patt=array("/，/","/\//","/-/");
	$data[actor]= preg_replace($patt,',',$data[actor]);
	//添加明星贴吧
	$actors=preg_split("/[\s,]+/",$data[actor]);
	foreach($actors as $actor){
		$key=$actor;$fieldid=0;
		$wheresql = "main.tagname='$key' AND main.fieldid='$fieldid'";
		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('mtag')." main WHERE $wheresql LIMIT 1");
		$mtag = $_SGLOBAL['db']->fetch_array($query);
		if(!$mtag) {
			//创建
			$mtag = array(
				'tagname' => $key,
				'fieldid' => $fieldid
			);
			$tagid = inserttable('mtag', $mtag, 1);
			$mtag['tagid'] = $tagid;
		} else {
			$tagid = $mtag['tagid'];
		}
	}
	$data[actor]=implode(',',$actors);
	$data[checked] = $_POST[check]?$_POST[check]:0;
	//$data[dateline] = time();
	$data[desrcipt] = $_POST[message];
	if ($_POST[movieid]) {
		//var_dump($movie);exit();
		//$data[checked] = 0;
		$wherearr = array('movieid'=>$_POST[movieid]);
		updatetable('app_movie',$data,$wherearr);
	
		$dataurl[movieurl] = trim($_POST[movieurl]); 
		$dataurl[playid] = $_POST[playid];
		$urlarr = explode("\n",$dataurl[movieurl]);
		$dataurl[movienum] = count($urlarr);
		
		if ($data[checked]==1){
			$_SGLOBAL['db']->query("UPDATE ".tname('app_movie_sendusers')." SET sendnum=sendnum+1 WHERE uid='$movie[uid]'");
			$_SGLOBAL['db']->query("UPDATE ".tname('space')." SET credit=credit+$_SGLOBAL[moviecredit] WHERE uid='$movie[uid]'");
		}
		
		
		updatetable('app_movieurl',$dataurl,$wherearr);
		showmessage('电影信息更新成功','space.php?do=movie&id='.$_POST[movieid]);
	}else {
		$data[uid] = $_SGLOBAL[supe_uid];
		$data[username] = $_SGLOBAL[supe_username];
		$dataurl[movieid] = '';

		$query = $_SGLOBAL['db']->query("SELECT movieid FROM ".tname('app_movie')." WHERE mname = '$data[mname]'");
		if ($result = $_SGLOBAL['db']->fetch_array($query)) {
			//var_dump($result);
			//showmessage('对不起，此影片已经存在');
			//exit();
			$id =$result['movieid'];
		}else{
			//添加专题
			
			$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('topic')." WHERE subject='$data[mname]'");
			$topic = $_SGLOBAL['db']->fetch_array($query);
			if($topic){
				$topicid=$topic['topicid'];
			}else{

			$setarr = array(
				'subject' => getstr($data[mname], 80, 1, 1, 1),
				'message' => getstr($data[desrcipt], 0, 1, 1, 1),
				'jointype' =>'blog,pic,thread,poll,event,share',
				'joingid' => '1,2,8,9,3,4,5,6,7',
				'endtime' => 0
			); 
			//封面
			if($data['images']!='') {
				$setarr['pic'] =str_replace($_SC['attachdir'],"",$data['images']);
				$setarr['thumb'] = 0;
				$setarr['remote'] =0;
			}
			 
			$setarr['uid'] = $_SGLOBAL['supe_uid'];
			$setarr['username'] = $_SGLOBAL['supe_username'];
			$setarr['dateline'] = $setarr['lastpost'] = $_SGLOBAL['timestamp'];	
			$topicid = inserttable('topic', $setarr, 1);
			}
		 	$data[topicid] = $topicid;
			$id = inserttable('app_movie',$data,1);
		}
		
		$dataurl[movieurl] = trim($_POST[movieurl]); 
		$dataurl[playid] = $_POST[playid];
		$urlarr = explode("\n",$dataurl[movieurl]);
		$dataurl[movienum] = count($urlarr);
		$dataurl[movieid] = $id;
		

		inserttable('app_movieurl',$dataurl);
		
		//var_dump($movie);exit();
		if ($data[checked]==1) {
			$query = $_SGLOBAL['db']->query("SELECT seid,sendnum FROM ".tname('app_movie_sendusers')." WHERE uid = '$data[uid]'");
			if ($seresult = $_SGLOBAL['db']->fetch_array($query)) {
				$query = $_SGLOBAL['db']->query("UPDATE ".tname('app_movie_sendusers')." SET sendnum=sendnum+1 WHERE uid = '$data[uid]'");
			}else {
				inserttable('app_movie_sendusers',array('uid'=>$data[uid],'username'=>$data[username],'sendnum'=>'1'));
			}
			$_SGLOBAL['db']->query("UPDATE ".tname('space')." SET credit=credit+$_SGLOBAL[moviecredit] WHERE uid='$data[uid]'");
		}
		
		showmessage('添加影片/资源成功','space.php?do=movie&id='.$id);
		//var_dump($dataurl);exit();
	}
}


if (submitcheck('sourcesubmit')){
	if(!checkperm('allowmovie')) {
		ckspacelog();
		showmessage('对不起，你没有权限操作');
	}
	
	if ($_POST[movieid]) {
		$dataurl[movieid] =$_POST[movieid];
		$dataurl[movieurl] = trim($_POST[movieurl]); 
		$dataurl[playid] = $_POST[playid];
		$urlarr = explode("\n",$dataurl[movieurl]);
		$dataurl[movienum] = count($urlarr);
		if($_POST[sourceid ]){		
			$wherearr = array('sourceid '=>$_POST[sourceid]);
			updatetable('app_movieurl',$dataurl,$wherearr);
		}else{
			inserttable('app_movieurl',$dataurl);
		}
		showmessage('添加影片/资源成功','space.php?do=movie&id='.$dataurl[movieid]);
		}else{
			
		showmessage('对不起，你要添加资源的电影不存在','space.php?do=movie');
		}
}

if ($op=='delete') {
	if(!checkperm('allowmovie')) {
		ckspacelog();
		showmessage('对不起，你没有权限操作');
	}
	$movieid = $_GET[movieid];
	if ($movieid) {
		
		@$_SGLOBAL['db']->query("DELETE FROM ".tname('app_movie')." WHERE movieid = '$movieid'");
		@$_SGLOBAL['db']->query("DELETE FROM ".tname('app_movieurl')." WHERE movieid = '$movieid'");
		//删除feed
		@$_SGLOBAL['db']->query("DELETE FROM ".tname('feed')." WHERE idtype='movieid' AND id='$movieid'");
		@$_SGLOBAL['db']->query("DELETE FROM ".tname('comment')." WHERE idtype='movieid' AND id='$movieid'");	
		//更新用户积分
		if ($movie[checked]==1) {
			$_SGLOBAL['db']->query("UPDATE ".tname('space')." SET credit=credit-$_SGLOBAL[moviecredit] WHERE uid='$movie[uid]'");
		}
		showmessage('删除电影成功','space.php?do=movie');
	}else {
		showmessage('对不起，你没有要删除的电影','space.php?do=movie');
	}
	
}
if($op=='source'){
	if(!checkperm('allowmovie')) {
		ckspacelog();
		showmessage('对不起，你没有权限操作');
	}
	
	$sourceid = empty($_GET['sourceid'])?0:intval($_GET['sourceid']);
	//$op = empty($_GET['op'])?'':$_GET['op'];

	$source = array();
	//$typelist = $regionlist = $playlist = array();

	if($sourceid) {
		$query = $_SGLOBAL['db']->query("SELECT * FROM " .tname('app_movieurl')." WHERE `sourceid`='$sourceid'");
		$source = $_SGLOBAL['db']->fetch_array($query);
	}
	include_once template("cp_video_source");
	exit;
}
include_once template("cp_video");

?>